/*
 * Powered By [leyvi]
 * Since 2014 - 2015
 */

var db = require('../../utils/db/mysqlDb');
var common = require('../../utils/commonUtils');
var config = require('../../../config');
var enFansModel = require('../model/EnFans');
var Promise = require('promise');

var enFansDb = {

};

var defaultSelectColumn = ' fans_id,account_code,fans_userid,fans_openid,fans_name,fans_department,fans_position,fans_mobile,fans_gender,fans_email,fans_weixinid,fans_avatar,fans_status,fans_extattr,fans_createtime,fans_updatetime';

var defaultTable = 'wechat_en_fans';
//getById
enFansDb.getById = function(id){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable+ ' where 1=1 ';
        if(typeof id === "object"){
            sql+='and fans_id= ' + db.escape(id.fansId);
        }else{
            sql+=' and fans_id= ' + db.escape(id);
        }
        db.query(sql).then(function(rows){
            if(rows[0]){
                fulfill(enFansModel.dbConvertModel(rows[0]));
            }else{
                fulfill(null);
            }             
        }, function(err){
            reject(err);
        });
    });
};

//save
enFansDb.save = function(obj, connection){
    return db.save('insert into ' + defaultTable+ '  set ?',
             enFansModel.modelConvertDb(obj), connection);
};

//update
enFansDb.update = function(obj, connection){
    var sql = 'update ' + defaultTable+ '  set ';
    if(common.isNotBlank(obj.accountCode)){
        sql+='account_code=:accountCode,';
    }
    if(typeof obj.fansUserid != "undefined"){
        sql+='fans_userid=:fansUserid,';
    }
    if(typeof obj.fansOpenid != "undefined"){
        sql+='fans_openid=:fansOpenid,';
    }
    if(typeof obj.fansName != "undefined"){
        sql+='fans_name=:fansName,';
    }
    if(typeof obj.fansDepartment != "undefined"){
        sql+='fans_department=:fansDepartment,';
    }
    if(common.isNotBlank(obj.fansPosition)){
        sql+='fans_position=:fansPosition,';
    }
    if(typeof obj.fansMobile != "undefined"){
        sql+='fans_mobile=:fansMobile,';
    }
    if(common.isNotBlank(obj.fansGender)){
        sql+='fans_gender=:fansGender,';
    }
    if(typeof obj.fansEmail != "undefined"){
        sql+='fans_email=:fansEmail,';
    }
    if(typeof obj.fansWeixinid != "undefined"){
        sql+='fans_weixinid=:fansWeixinid,';
    }
    if(typeof obj.fansAvatar != "undefined"){
        sql+='fans_avatar=:fansAvatar,';
    }
    if(common.isNotBlank(obj.fansStatus)){
        sql+='fans_status=:fansStatus,';
    }
    if(typeof obj.fansExtattr != "undefined"){
        sql+='fans_extattr=:fansExtattr,';
    }
    if(common.isNotBlank(obj.fansCreatetime)){
        sql+='fans_createtime=:fansCreatetime,';
    }
    if(common.isNotBlank(obj.fansUpdatetime)){
        sql+='fans_updatetime=:fansUpdatetime,';
    }
    sql+='where ';
        sql+='fans_id=:fansId ';
        sql = sql.replace(',where', '  where');
    return db.update(sql, obj, connection);
};

//delete
enFansDb.delete = function(id, connection){
    var sql = 'delete from ' + defaultTable+ '  where 1=1 ';
    if(typeof id === "object"){
        sql+=' and fans_id= ' + db.escape(id.fansId);
    }else{
        sql+=' and fans_id= ' + db.escape(id);
    }
    return db.update(sql, {}, connection);
};

//查询条件
var selectParams = function(params){
    var sql = '';
    if(params && typeof params.fansId != "undefined"){
        sql+=' and  fans_id=' + db.escape(params.fansId);
    }
    if(params && typeof params.accountCode != "undefined"){
        sql+=' and  account_code=' + db.escape(params.accountCode);
    }
    if(params && typeof params.fansUserid != "undefined"){
        sql+=' and  fans_userid=' + db.escape(params.fansUserid);
    }
    if(params && typeof params.fansOpenid != "undefined"){
        sql+=' and  fans_openid=' + db.escape(params.fansOpenid);
    }
    if(params && typeof params.fansName != "undefined"){
        sql+=' and  fans_name=' + db.escape(params.fansName);
    }
    if(params && typeof params.fansDepartment != "undefined"){
        sql+=' and  fans_department=' + db.escape(params.fansDepartment);
    }
    if(params && typeof params.fansPosition != "undefined"){
        sql+=' and  fans_position=' + db.escape(params.fansPosition);
    }
    if(params && typeof params.fansMobile != "undefined"){
        sql+=' and  fans_mobile=' + db.escape(params.fansMobile);
    }
    if(params && typeof params.fansGender != "undefined"){
        sql+=' and  fans_gender=' + db.escape(params.fansGender);
    }
    if(params && typeof params.fansEmail != "undefined"){
        sql+=' and  fans_email=' + db.escape(params.fansEmail);
    }
    if(params && typeof params.fansWeixinid != "undefined"){
        sql+=' and  fans_weixinid=' + db.escape(params.fansWeixinid);
    }
    if(params && typeof params.fansAvatar != "undefined"){
        sql+=' and  fans_avatar=' + db.escape(params.fansAvatar);
    }
    if(params && typeof params.fansStatus != "undefined"){
        sql+=' and  fans_status=' + db.escape(params.fansStatus);
    }
    if(params && typeof params.fansExtattr != "undefined"){
        sql+=' and  fans_extattr=' + db.escape(params.fansExtattr);
    }
    if(params && typeof params.fansCreatetime != "undefined"){
        sql+=' and  fans_createtime=' + db.escape(params.fansCreatetime);
    }
    if(params && typeof params.fansUpdatetime != "undefined"){
        sql+=' and  fans_updatetime=' + db.escape(params.fansUpdatetime);
    }
    return sql;
};


//count
enFansDb.count = function(params){
    return new Promise(function(fulfill, reject){
        var sql = 'select count(*) as total from ' + defaultTable;
        sql += ' where 1=1 ';
        sql += selectParams(params);
        db.query(sql).then(function(rows){
            fulfill(rows[0].total);
        }, function(err){
            reject(err);
        });
    });
};

//page
enFansDb.getByPage = function(params, pageNo, pageSize, orderby){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
        sql+=' where 1=1 ';
        sql += selectParams(params);
        sql+=' order by ' + orderby;
        if(pageNo <= 0){
            pageNo = 1;
        }
        pageNo = common.isNumber(pageNo) ? pageNo : 1;
        pageSize = common.isNumber(pageSize) ? pageSize : 10;
        sql += ' limit ';
        sql += (pageNo - 1) * pageSize;
        sql +=',';
        sql +=pageSize;
       
        db.query(sql).then(function(rows){
            var array = new Array();
            for(var p in rows){
                if(rows[p])array.push(enFansModel.dbConvertModel(rows[p]));
            }
            fulfill(array);
        }, function(err){
            reject(err);
        });
    });
};

enFansDb.getByParams = function(params, orderby){
    return new Promise(function(fulfill, reject){
        var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
        sql+=' where 1=1 ';
        sql += selectParams(params);
        sql+=' order by ' + orderby;
        db.query(sql).then(function(rows){
            var array = new Array();
            for(var p in rows){
                if(rows[p])array.push(enFansModel.dbConvertModel(rows[p]));
            }
            fulfill(array);
        }, function(err){
            reject(err);
        });
    });
};

enFansDb.db = db;
module.exports = enFansDb;